package com.vieewer.btdemo.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.BatteryManager;
import android.util.Base64;
import android.util.Log;

import java.io.*;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.List;

public class CommonUtils {

    /**
     * 根据WIFI
     * 获取本机IPv4地址
     *
     * @param context
     * @return 本机IPv4地址；null：无网络连接
     */
    public static String getIpAddress(Context context) {
        // 获取WiFi服务
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        // 判断WiFi是否开启
        if (wifiManager.isWifiEnabled()) {
            // 已经开启了WiFi
            Log.i("tag","已经开启了WIFI");
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int ipAddress = wifiInfo.getIpAddress();
            Log.i("tag","我的ipAddress："+ipAddress);
            String ip = intToIp(ipAddress);
            Log.i("tag","我的IP："+ip);
            return ip;
        } else {
            // 未开启WiFi
            return getIpAddress();
        }
    }

    private static String intToIp(int ipAddress) {
        return (ipAddress & 0xFF) + "." +
                ((ipAddress >> 8) & 0xFF) + "." +
                ((ipAddress >> 16) & 0xFF) + "." +
                (ipAddress >> 24 & 0xFF);
    }

    /**
     * 根据2G/3G
     * 获取本机IPv4地址
     * @return 本机IPv4地址；null：无网络连接
     */
    private static String getIpAddress() {
        try {
            NetworkInterface networkInterface;
            InetAddress inetAddress;
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                networkInterface = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = networkInterface.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
            return null;
        } catch (SocketException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public static List<File> getFile(File file) {
        List<File> mFileList=new ArrayList<>();
        File[] fileArray = file.listFiles();
        if (fileArray != null) {
            for (File f : fileArray) {
                if (f.isFile()) {
                    mFileList.add(f);
                } else {
                    getFile(f);
                }
            }
        }
        return mFileList;
    }

    /**
     * 根据当前日期获得是星期几
     * time=yyyy-MM-dd
     * @return
     */
    public static String getWeek(String time) {
        String Week = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        int wek=c.get(Calendar.DAY_OF_WEEK);

        if (wek == 1) {
            Week += "星期日";
        }
        if (wek == 2) {
            Week += "星期一";
        }
        if (wek == 3) {
            Week += "星期二";
        }
        if (wek == 4) {
            Week += "星期三";
        }
        if (wek == 5) {
            Week += "星期四";
        }
        if (wek == 6) {
            Week += "星期五";
        }
        if (wek == 7) {
            Week += "星期六";
        }
        return Week;
    }


    public static String getformatedCode(String language) {
        String code = language;
        if (language.contains("zh")) {
            code = "zh-Hans";
        } else {
            if (language.contains("-")) {
                int index = language.indexOf("-");
                code = language.substring(0, index);
            }
        }
        Log.i("yjj", "CommonUtils getformatedCode =" + code);
        return code;
    }




    //编码：
    public static String encodeImageToString(Bitmap bitmap) {

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        //读取图片到ByteArrayOutputStream

        bitmap.compress(Bitmap.CompressFormat.PNG, 40, outputStream); //参数如果为100那么就不压缩

        byte[] bytes = outputStream.toByteArray();

        String strImg = Base64.encodeToString(bytes, Base64.DEFAULT);

        return strImg;

    }



    // 解码：
    public static Bitmap sendImage(String imgAddress) {

        byte[] input = Base64.decode(imgAddress, Base64.DEFAULT);

        Bitmap bitmap = BitmapFactory.decodeByteArray(input, 0, input.length);

        return bitmap;

    }



    //sys_path 为节点映射到的实际路径
    public static String readFile(String sys_path) {
        String prop = "0";// 默认值
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(sys_path));
            prop = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
            Log.w("yjj", " ***ERROR*** Here is what I know: " + e.getMessage());
        } finally {
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        Log.w("yjj", "readFile cmd from" + sys_path + "data"+" -> prop = " + prop);
        return prop;
    }


    //sys_path 为节点映射到的实际路径
    public static String read(String sys_path){
        try {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec("cat " + sys_path); // 此处进行读操作
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line ;
            while (null != (line = br.readLine())) {
                Log.w("yjj", "read data ---> " + line);
                return line;
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.w("yjj", "*** ERROR *** Here is what I know: " + e.getMessage());
        }
        return null;
    }



    //public static int getSecondBattery() {
/*
        //通过属性获取第二块电池信息：
        int level = SystemProperties.getInt("sys.battery_second_level", 50);
        int status = SystemProperties.getInt("sys.battery_second_status", -1);
        int pluginType = SystemProperties.getInt("sys.battery_second_plugin", -1);
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                status == BatteryManager.BATTERY_STATUS_FULL;*/

    //}



}
